package com.example.administrator.wallet.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.model.bean.WebSocketPendingOrderBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2018/8/30 0030.
 */

public class CurrentDelegateFragmentAdapter extends BaseAdapter {

    private Context context;
    private List<WebSocketPendingOrderBean.ResultBean.RecordsBean> list;
    private CurrentDelegateFragmentAdapterViewHolder holder;

    public CurrentDelegateFragmentAdapter(Context context, List<WebSocketPendingOrderBean.ResultBean.RecordsBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            holder = new CurrentDelegateFragmentAdapterViewHolder();
            convertView = View.inflate(context, R.layout.item_current_delegation,null);

            holder.tvBuyOrSell = (TextView) convertView.findViewById(R.id.tv_buy_or_sell_current_delegation_list);
            holder.tvMarket = (TextView) convertView.findViewById(R.id.tv_market_current_delegation_list);
            holder.tvAmount = (TextView) convertView.findViewById(R.id.tv_amount_current_delegation_list);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price_current_delegation_list);
            holder.tvAmountPrice = (TextView) convertView.findViewById(R.id.tv_amount_price_current_delegation_list);
            holder.tvCancel = (TextView) convertView.findViewById(R.id.tv_cancel_current_delegation_list);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time_current_delegation_list);

            convertView.setTag(holder);
        } else {
            holder = (CurrentDelegateFragmentAdapterViewHolder) convertView.getTag();
        }

        if (list.get(position).getType() == 1){
            holder.tvBuyOrSell.setText(context.getResources().getString(R.string.sell));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.tvBuyOrSell.setBackground(context.getDrawable(R.drawable.shape_current_delegation_sell));
                }
            }
        } else {
            holder.tvBuyOrSell.setText(context.getResources().getString(R.string.buy));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.tvBuyOrSell.setBackground(context.getDrawable(R.drawable.shape_current_delegation_buy));
                }
            }
        }

        holder.tvMarket.setText(list.get(position).getMarket());

        String strAmount = list.get(position).getAmount();
        double amount = Double.parseDouble(strAmount);
        double left = Double.parseDouble(list.get(position).getLeft());
        holder.tvAmount.setText((amount - left) + "/" + strAmount);

        String strPrice = list.get(position).getPrice();
        double price = Double.parseDouble(strPrice);

        holder.tvPrice.setText(strPrice);

        double amountPrice = amount*price;

        holder.tvAmountPrice.setText(amountPrice + "");

        long time = (long) list.get(position).getMtime() * 1000;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        holder.tvTime.setText(simpleDateFormat.format(date));

        holder.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return convertView;
    }

    public static class CurrentDelegateFragmentAdapterViewHolder{
        TextView tvBuyOrSell,tvMarket,tvAmount,tvPrice,tvAmountPrice,tvCancel,tvTime;
    }
}
